#ifndef __MY_NV_INTERNAL
#define __MY_NV_INTERNAL

#include "HCFGStorage.h"

/*  Groupping types
*/
enum tpGroups {
 GRP_SITE = 1, //By Site
 GRP_ADDR = 2, //By address
 GRP_DATE = 3,  //By date
 GRP_NONE = 0
};

/*  Split file formats
*/
enum tpSplitFormats {
  sfSquid,
  sfNaviscope
};

/* Sort columns
*/
#define SRT_SITE   1
#define SRT_URL    2
#define SRT_ADDR   3
#define SRT_SIZE   4
#define SRT_DATE   5
#define SRT_SITE_I -SRT_SITE
#define SRT_URL_I  -SRT_URL
#define SRT_ADDR_I -SRT_ADDR
#define SRT_SIZE_I -SRT_SIZE
#define SRT_DATE_I -SRT_DATE

/*  UrlInfo
*/
#define SITE_STRING_LEN    30
typedef StatString<SITE_STRING_LEN> SiteString;

#define URL_STRING_LEN     2*1024
typedef StatString<URL_STRING_LEN> UrlString;

#define HOST_STRING_LEN    200
typedef StatString<HOST_STRING_LEN> HostString;

LOCALSTRUCT( UrlInfo )
  SiteString Site;      //Requested address (www)
  UrlString  Url;       //Path on requested address (file)
  HostString Host;      //Requestor address (local)
  __int64    Size;      //Transfer size
  PRTime     Time;      //Request time


  UrlInfo( void ) {}

  void     Set( tpGroups tp,CONSTSTR txt );
  int      CmpTime( const UrlInfo& p, bool Filter ) const;
  bool     SetCTime( char *str );
  bool     Load( CTParser& in,AnsiString& error );
  CONSTSTR TypeStr( tpGroups tp );
};

LOCALCLASSBASE( InfoArray, public MyRefArray<UrlInfo> )
  public:
   InfoArray( void ) : MyRefArray<UrlInfo>( 5000, 10000 ) {}

  DEF_SCANER( PUrlInfo, TotalSize, __int64*, { *Data += Item->Size; return false; } )
};
//---------------------------------------------------------------------------
STRUCT( GroupInfo )
   __int64 Count;
   __int64 Size;
};

typedef HBNameObjectTree<GroupInfo,MyString> Names, *PNames;
typedef HBNameObjectTree<PUrlInfo,MyString>  CollapseNames, *PCollapseNames;

//---------------------------------------------------------------------------
LOCALSTRUCT( NameInfo )
   StatString<4*3+3+1> Addr;
   StatString<50>      Name;

   NameInfo( void ) {}
   NameInfo( CONSTSTR addr,CONSTSTR nm ) : Addr(addr),Name(nm) {}
};

LOCALCLASSBASE( NameArray, public MyRefArray<NameInfo> )
  public:
   NameArray( void ) : MyRefArray<NameInfo>( 50,50 ) {}

   void     Load( void );
   void     Save( void );
   CONSTSTR DeName( CONSTSTR str );
    void    SortAddr( void );
    int     FindAddr( const NameInfo& ni );
};

/* Importer defines
*/
typedef bool __fastcall (__closure *AddUrlItem_p)( const UrlInfo& p );

LOCALSTRUCT( ImportDef )
   CONSTSTR Filter;
   CONSTSTR MenuTitle;
   void __fastcall (*Proc)( HWND Handle, HOperation& rop, PRPeriod& h, CONSTSTR FileName, AddUrlItem_p cb );
};

//---------------------------------------------------------------------------
LOCALSTRUCT( FileInfo )
  FILE       *File;
  PathString  Name,
              FileName;

  FileInfo( CONSTSTR path,CONSTSTR nm ) {
    Name     = nm;
    FileName = path;
    FileName.Add( '\\' );
    FileName.Add( nm );

    File = fopen( FileName.Text(),"a+" );
  }
  ~FileInfo() { if (File) fclose(File); }
};

LOCALCLASSBASE( FileArray, public MyArray<PFileInfo> )
  public:
   FileArray( void ) : MyArray<PFileInfo>( 50,TRUE,50 ) {}

  DEF_SCANER( PFileInfo, Find, CONSTSTR, { return Item->Name.Cmp(Data); } )
};


//---------------------------------------------------------------------------
//[nv_Names.cpp]
void EditNames( NameArray& al );

//[nv_Cfg.cpp]
void Configure( void );

//[nv_Utils.cpp]
typedef const UrlInfo *Sort_t;
#define SORTI( v ) (v)

extern int RTL_CALLBACK idSortLoad( Sort_t left, Sort_t right );

extern int RTL_CALLBACK idSortSite( Sort_t left, Sort_t right );
extern int RTL_CALLBACK idSortAddr( Sort_t left, Sort_t right );
extern int RTL_CALLBACK idSortSize( Sort_t left, Sort_t right );
extern int RTL_CALLBACK idSortDate( Sort_t left, Sort_t right );
extern int RTL_CALLBACK idSortSiteI( Sort_t left, Sort_t right );
extern int RTL_CALLBACK idSortAddrI( Sort_t left, Sort_t right );
extern int RTL_CALLBACK idSortSizeI( Sort_t left, Sort_t right );
extern int RTL_CALLBACK idSortDateI( Sort_t left, Sort_t right );

//[nv_LoadNV.cpp]
void __fastcall LoadNaviscope( HWND Handle, HOperation& rop, PRPeriod& h, CONSTSTR nm, AddUrlItem_p cb );

//[nv_LoadSqN.cpp]
void __fastcall LoadSquid( HWND Handle, HOperation& rop, PRPeriod& h, CONSTSTR nm, AddUrlItem_p cb );

//[nv_LoadTraf.cpp]
void __fastcall LoadTraf( HWND Handle, HOperation& rop, PRPeriod& h, CONSTSTR nm, AddUrlItem_p cb );

#define GOPT( nm, def )  Opt->Read( nm, def )
#define SOPT( nm, val )  Opt->Write( nm, val )

extern PHStorageLink Opt;

#endif
